package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public void saveOrUpdate(User user) {
        if(user.getId() == null){
            User temp = userMapper.selectByEmailAndPassword(user.getEmail(), null);
            if (temp != null) {
                throw new RuntimeException("该邮箱已存在");
            }
            user.setHeadImgUrl("/img/user/head.jpg");
            user.setCoverImgUrl("/img/user/bg.jpeg");
            user.setGender(User.GENDER_SECRET);
            userMapper.insert(user);
        }else {
            userMapper.updateByPrimaryKey(user);
        }
    }

    @Override
    public User selectEmailAndPassword(String email, String password) {
        User user = userMapper.selectByEmailAndPassword(email, password);
        if (user == null) {
            throw new RuntimeException("账号密码不匹配");
        }
        return user;
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> users = userMapper.queryForList(qo);
        return new PageInfo(users);
    }

    @Override
    public boolean selectAttention(Long fansId,Long attentionId) {
        return userMapper.selectRelation(fansId,attentionId) == null;
    }

    @Override
    public void saveRelation(Long fansId, Long attentionId) {
        if (userMapper.selectRelation(fansId,attentionId) == null) {
            userMapper.insertRelation(fansId,attentionId);
        }

    }

    @Override
    public void deleteRelation(Long fansId, Long attentionId) {
        userMapper.deleteRelation(fansId,attentionId);
    }

    @Override
    public Map<String,Object> queryNum(Long id) {
        Map<String,Object> map = new HashMap<>();
        Map fansMap = userMapper.queryFans(id);
        map.put("fansNum",fansMap.get("fansNum"));
        Map attentionMap = userMapper.queryAttention(id);
        map.put("attentionNum",attentionMap.get("attentionNum"));
        return map;
    }




    @Override
    public boolean getCollectRelation(Long userId, Long travelId) {
        return userMapper.selectCollectRelation(userId,travelId) == null;
    }

    @Override
    public void deleteCollect(Long userId, Long travelId) {
        userMapper.delectCollect(userId,travelId);
    }

    @Override
    public void saveCollect(Long userId, Long travelId) {
        userMapper.insertCollect(userId,travelId);
    }

    @Override
    public Object listLike(User user) {
        return userMapper.listLike(user);
    }
    @Override
    public User queryUserById(Long userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        return user;
    }

    @Override
    public User selectAllProducts(Long userId) {
       return userMapper.queryAllProducts(userId);
    }

    @Override
    public PageInfo queryTravelList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Map<String,Object>> list = userMapper.queryMyStrategy(qo);
        return new PageInfo(list);

    }

    @Override
    public Boolean queryIsExist(Long userId, Long strategyId) {

        String  s = userMapper.selectIsExist(userId, strategyId);
        if (s == null) {
            return false;
        }
        return true;
    }

    @Override
    public void saveTravelList(Long userId, Long strategyId) {
        userMapper.insertTravlList(userId,strategyId);
    }

}
